//import java.util.HashMap;
//import java.util.Arrays;
//import java.util.Map;
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null||head.next==null||head.next.next==null)
            return null;
        //判断是否有环，如果有在第一次相遇时停止
        ListNode slow=head.next;
        ListNode fast=head.next.next;
        while(slow!=fast){
            if(fast.next==null||fast.next.next==null){
                return null;
            }
            slow=slow.next;
            fast=fast.next.next;
        }
        fast=head;
        while(fast!=slow){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
}